home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / pdf / c++ / Password < prev    next >
Text File  |  2003-02-14  |  4KB  |  128 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #include "password.h"
  39. #include "GuiWindow.h"
  40. #include "GuiGadget.h"
  41. #include "UserEvents.h"
  42. #include "GuiTargets.h"
  43.  
  44. class Password
  45. {
  46.   public:
  47.     Password();
  48.     ~Password();
  49.     bool get(string& user,string& owner);
  50.  
  51.   private:
  52.     GuiWindow        window;
  53.     GuiWritableField password;
  54.     GuiOptionButton  isOwner;
  55.     enum {READY,BUSY,FINISHED,CANCELLED} status;
  56.  
  57.     GUI_DECLARE_EVENT_TARGETS(Password); 
  58.  
  59.     GuiToolboxTarget okPressedTarget;
  60.     Claim okPressed(GuiToolboxEvent&,const GuiIdBlock&);
  61.  
  62.     GuiNullTarget nullEventTarget;
  63.     void nullEvent();
  64. };
  65.  
  66. //*************************************************************************
  67.  
  68. Password::Password()
  69.   : window("Password"),
  70.     password(window,1),
  71.     isOwner(window,6),
  72.     okPressedTarget(&window,User_Password,this,Password::okPressed),
  73.     status(READY)
  74. {
  75. }
  76.  
  77. //*************************************************************************
  78.  
  79. Password::~Password() {}
  80.  
  81. //*************************************************************************
  82.  
  83. bool Password::get(string& user,string& owner)
  84. {
  85.   user.clear();
  86.   owner.clear();
  87.  
  88.   if (status!=READY) return 0;
  89.  
  90.   window.showCentred();
  91.   password.setValue("");
  92.   isOwner.set(0);
  93.   status=BUSY;
  94.   nullEventTarget(this,Password::nullEvent);
  95.   while (status==BUSY) GuiRegister::poll();
  96.   window.hide();
  97.   nullEventTarget.destroy();
  98.   user=password.getValue();
  99.   if (isOwner.isOn()) {owner=user;user.clear();}
  100.   if (status==CANCELLED) {status=READY;return 0;}
  101.   status=READY;
  102.   return 1;
  103. }
  104.  
  105. //*************************************************************************
  106.  
  107. void Password::nullEvent()
  108. {
  109.   if (status==BUSY && !window.isShowing()) status=CANCELLED;
  110. }
  111.  
  112. //*************************************************************************
  113.  
  114. Claim Password::okPressed(GuiToolboxEvent&,const GuiIdBlock&)
  115. {
  116.   status=FINISHED;
  117.   return CLAIM;
  118. }
  119.  
  120. //*************************************************************************
  121.  
  122. bool getPassword(string& user,string& owner)
  123. {
  124.   static Password p;
  125.   return p.get(user,owner);
  126. }
  127.  
  128.